#include "stdafx.h"
#include "envexplorer.h"
#include "QEnvPath.h"
#include "QEnvStatus.h"
#include "QCmdLine.h"
	
EnvExplorer::EnvExplorer(QWidget *parent)
	: QMainWindow(parent)
	, mStyle( NULL )
{
	ui.setupUi(this);
	ui.varTable->setCustom( ui.litTable );
	connect( ui.searchText, SIGNAL(textChanged(const QString&)), this, SLOT(on_searchText_modified(const QString &)) );
}

EnvExplorer::~EnvExplorer()
{

}
void EnvExplorer::on_searchText_modified(const QString &str)
{
	if( str.isEmpty() ) ui.varTable->clearFilters();
	else ui.varTable->setFilter( String(str) );
}
void EnvExplorer::initialise()
{
	QEnvStatus::_register( this, ui.statusBar, ui.logEdit );
	
#ifndef NDEBUG
	qApp->setApplicationDisplayName(tr("Environnement Explorer (Debug)"));
	qApp->setApplicationName(tr("env_explorer_d"));
	qApp->setApplicationVersion("0.1.0");
#else
	qApp->setApplicationDisplayName(tr("Environnement Explorer"));
	qApp->setApplicationName(tr("env_explorer"));
	qApp->setApplicationVersion("0.1.0");
#endif
	qApp->setWindowIcon( QIcon( ":/Resources/variables.png" ) );
	setIconSize( QSize( 64, 64 ) ); 

	QStyleFactory fac;
	QFile styleFile( ":/Styles/dark.qss" );
	styleFile.open( QFile::ReadOnly );

	QString style( styleFile.readAll() );
	qApp->setStyleSheet( style );

	QProcConfig config = QCmdLine::Instance().getConfig();

	QEnvStatus::addStatus( String(tr("Running command line")+" (path: "+(QCmdLine::Instance().arguments().empty()?String():QCmdLine::Instance().arguments().at(0))+") :\r\n\t") + QCmdLine::Instance().arguments().join("\r\n\t") );
	if( config.errors & QCMD_ERROR )
	{
		String err = String(tr("There were some errors while configuring the app, running base config:")+"\r\n");
		if( config.errors&QCMD_EMPTY ) err += (tr("[+] Command is Empty\r\n"));
		if( config.errors&QCMD_QUIT ) err += (tr("[-] Command returned CMD_ERRQUIT"));
		QMessageBox::warning( this, tr("Configuration Error"), err );
	}
	else QEnvStatus::addStatus( String("Success !") );


	try{ ui.varTable->grabEnv(); }
	catch( std::exception ex ){ QEnvStatus::addMessage(FROMQT(tr("Cannot grab path environnement ..."))); }
}
void EnvExplorer::on_actionImport_Env_triggered()
{
	ui.varTable->grabEnv();
}
void EnvExplorer::on_actionExport_Env_triggered()
{
	ui.varTable->saveEnv();
}